<?php

namespace App\Http\Controllers\Mobile;


use App\Logics\CollectLogic;
use App\Logics\FanLogic;
use App\Logics\UserAgentLogic;
use App\Logics\UserAuthLogic;
use App\Logics\UserLogic;


class ProfileController extends BaseController
{
    // 个人资料（必须登录）
    public function initialize()
    {
        parent::initialize();
    }

    // 个人资料
    public function info()
    {
        $info = (new UserLogic())->info($this->myUid);

        return $this->success($info);
    }

    // 完善个人资料
    public function profile()
    {
        $logic = new UserLogic();
        $info  = $logic->profile($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success($info, '资料完善成功！');
    }

    // 修改密码
    public function password()
    {
        $logic = new UserLogic();
        $info  = $logic->password($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success($info, '修改密码成功！');
    }

    // 注销
    public function cancel()
    {
        $logic = new UserLogic();
        $info  = $logic->cancel($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success([], '注销成功！');
    }


    ####################################################################################################
    // 关注认证的机构
    public function onFollow()
    {
        $logic = new FanLogic();
        $res   = $logic->onFollow($this->myUid);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success([], $res ? '关注成功！' : '取消关注成功！');
    }

    // 收藏：各种文章活动
    public function onCollect()
    {
        $logic = new CollectLogic();
        $res   = $logic->onCollect($this->myUid);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success([], $res ? '收藏成功！' : '取消收藏成功！');
    }

    // 我的收藏
    public function myCollect()
    {
        $logic = new CollectLogic();
        $res   = $logic->onLists($this->myUid);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success($res);
    }

    // 我的关注
    public function myFollow()
    {
        $logic = new FanLogic();
        $res   = $logic->onLists($this->myUid);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success($res);
    }

    ####################################################################################################
    ## 认证资料

    // 认证
    public function onAuth()
    {
        $logic = new UserAuthLogic();
        $info  = $logic->onAuth($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success([], '认证提交成功，请等待审核！');
    }

    // 删除认证：方便测试
    public function delAuth()
    {
        $logic = new UserAuthLogic();
        $info  = $logic->delAuth($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success([], '删除认证资料成功！');
    }

    // 我的认证资料
    // 获取审核不通过的原因
    public function auth()
    {
        $user = $this->myUser;
        if(empty($user['auth_id'])){
            return $this->error('请先进行认证！');
        }

        $logic = new UserAuthLogic();
        $info  = $logic->onShow($user['auth_id']);
        if($info === false){
            return $this->error($logic->getError());
        }

        //前端要求： 提交认证 但没有审核的时候 需要返回空
        /*if(($info['verify'] == 0) && ($info['is_auth'] == 1)){
            $info = [];
        }*/

        return $this->success($info);
    }

    // 代理
    public function onAgent()
    {
        $logic = new UserAgentLogic();
        $info  = $logic->onAgent($this->myUid);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success([], '认证提交成功，请等待审核！');
    }

    // 我的代理资料
    public function agent()
    {
        $user = $this->myUser;
        if(empty($user['auth_id'])){
            return $this->error('请先进行认证！');
        }

        $logic = new UserAgentLogic();
        $info  = $logic->onShow($user['auth_id']);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success($info);
    }

}


